import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import {cookie} from "@/utils/cookie.ts";

import Home from "@/views/Home.vue";

const route: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: () => {
            return {
                path: '/home',
            }
        },
    },
    {
        path: '/404',
        name: 'NoPage404',
        component: () => import('@/components/404.vue'),
    },
    {
        path: '/:pathMatch(.*)',
        redirect: '/404',
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
    },
    {
        path: '/program/:id/:pathMatch(.*)*',
        component: () => import('@/components/program/Detail.vue'),
        children: [
            {
                path: '/program/desc/:id/:pathMatch(.*)*',
                component: () => import('@/components/program/Description.vue')
            },
            {
                path: '/program/api/:id/:pathMatch(.*)*',
                component: () => import('@/components/Sider.vue'),
                children: [
                    {
                        path: '/program/api/add/:id/:pathMatch(.*)*',
                        component: () => import('@/components/api/Add.vue'),
                    },
                    {
                        path: '/program/api/all/:id/:pathMatch(.*)*',
                        component: () => import('@/components/api/List.vue'),
                    },
                    {
                        path: '/program/api/show/:id/:pathMatch(.*)*',
                        component: () => import('@/components/api/Show.vue'),
                    }
                ]
            },
            {
                path: '/program/invite/:id/:pathMatch(.*)*',
                component: () => import('@/views/Invited.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/Login.vue'),
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('@/views/Register.vue')
    },
    {
        path: '/invited/:id',
        name: 'invited',
        component: () => import('@/views/Invited.vue')
    },
    {
        path: '/join/:token',
        component: () => import('@/views/Join.vue')
    }
]


const router = createRouter({
    history: createWebHashHistory(),
    // 返回时记录原先页面位置
    scrollBehavior: (to, from, savePosition) => {
        if (savePosition) {
            return savePosition;
        } else {
            return {
                // 距离页面上方
                top: 0
            }
        }
    },
    routes: route
})


// 前置路由守卫
// 路由守卫
router.beforeEach((to, from, next) => {
    const token = cookie.get("token");
    if (to.name === 'register') {
        next();
        return;
    }
    if (!token && 'login' !== to.name) {
        console.log("已阻止");
        next({name: 'login'})
    } else {
        // 重新登录
        if (to.name === 'login') {
            cookie.remove("token");
        }
        next();
    }
})
export default router;